﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;

namespace YJKArchMethodLibrary.Utils.SelectMaterial
{
	public partial class FillPatternListForm : System.Windows.Forms.Form
	{
		public FillPatternListForm(Autodesk.Revit.DB.Document doc, FillPattern currentFillPattern)
		{
			this.m_Doc = doc;
			this.m_OldFillPattern = currentFillPattern;
			this.InitializeComponent();
		}

		private void FillPatternListForm_Load(object sender, EventArgs e)
		{
			this.UpdatePatternList();
		}

		public FillPattern SelectedFillPattern()
		{
			int index = this.dbgFillPatternList.SelectedRows[0].Index;
			return this.m_FillPattenList[index];
		}

		private void UpdatePatternList()
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Doc);
			ICollection<Element> collection = filteredElementCollector.OfClass(typeof(FillPatternElement)).ToElements();
			this.m_FillPattenList.Clear();
			this.dbgFillPatternList.Rows.Clear();
			int num = -1;
			int num2 = 0;
			foreach (Element element in collection)
			{
				FillPatternElement fillPatternElement = element as FillPatternElement;
				FillPattern fillPattern = fillPatternElement.GetFillPattern();
				int num3 = this.m_FillPattenList.FindIndex((FillPattern value) => value.Name.CompareTo(fillPattern.Name) == 0);
				if (num3 == -1)
				{
					this.m_FillPattenList.Add(fillPattern);
				}
			}
			this.m_FillPattenList.Sort(new FillPatternComparer());
			foreach (FillPattern fillPattern2 in this.m_FillPattenList)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				dataGridViewRow.Height = 40;
				DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell.Value = fillPattern2.Name;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);
				DataGridViewImageCell dataGridViewImageCell = new DataGridViewImageCell();
				dataGridViewImageCell.Value = FillPatternDrawingKit.CreateFillPatternImage(fillPattern2, (double)this.ColumnImage.Width, (double)dataGridViewRow.Height);
				dataGridViewRow.Cells.Add(dataGridViewImageCell);
				this.dbgFillPatternList.Rows.Add(dataGridViewRow);
				if (this.m_OldFillPattern != null && fillPattern2.IsEqual(this.m_OldFillPattern))
				{
					num = num2;
				}
				num2++;
			}
			if (-1 != num)
			{
				this.dbgFillPatternList.Rows[num].Selected = true;
			}
		}

		private void FillPatternListForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.Cancel)
			{
				return;
			}
			if (this.dbgFillPatternList.SelectedRows.Count == 0)
			{
				MessageBox.Show("没有选中填充类型", "盈建科提示");
				e.Cancel = true;
			}
		}

		private Document m_Doc;

		private List<FillPattern> m_FillPattenList = new List<FillPattern>();

		private FillPattern m_OldFillPattern;
	}
}
